package com.example.tallybook;

import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import com.example.tallybook.adapter.RecordPageAdapter;
import com.example.tallybook.frag_record.IncomeFragment;
import com.example.tallybook.frag_record.OutcomeFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;


public class RecordActivity extends AppCompatActivity {
    TabLayout tabLayout;
    ViewPager2 viewPager2;
    @Override
    protected  void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        /*查找控件*/
        tabLayout=findViewById(R.id.record_tabs);
        viewPager2=findViewById(R.id.record_vp);
        /*设置viewpager加载的页面*/
        initPager();
    }
    private void initPager(){
        List<Fragment>fragmentList=new ArrayList<>();
        OutcomeFragment outFrag=new OutcomeFragment();
        IncomeFragment inFrag=new IncomeFragment();
        fragmentList.add(outFrag);
        fragmentList.add(inFrag);
        //创建适配器
        RecordPageAdapter recordPageAdapter=new RecordPageAdapter(this,fragmentList);
        //设置适配器
        viewPager2.setAdapter(recordPageAdapter);
        //将tablelayout与viewpager关联
        new TabLayoutMediator(tabLayout,viewPager2,((tab, position) ->{
            if(position==0){
                tab.setText("支出");
            } else if (position==1) {
                tab.setText("收入");
            }
        } )).attach();
    }
    /*点击时间*/
    public void onClick(View view){
        switch(view.getId()){
            case R.id.record_iv_back:
                finish();
                break;
        }
    }
}
